<?php
namespace Home\Controller;
use  Think\Controller;

class NoticeController extends CommonController{

    public function index(){

        $id = session(C(SESSIONUSER));

        $where['userId'] = $id;

        // 导入分页类
        import('Org.Util.Page');

        $res_notice = M('notice')->where($where)->select();

        //查询满足满足条件的总记录条数
        $count = count($res_notice);

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,2);

        //分页显示输出
        $show = $Page->show();
        $limit = $Page->firstRow . ',' . $Page->listRows;

        $res_notice1 = M('notice')->where($where)->limit($limit)->order('time desc')->select();

        foreach($res_notice1 as $k=>$v){

            $res_notice1[$k]['time'] = date('Y-m-d',$v['time']);

            if($v['allow'] == 2){
                $res_notice1[$k]['allow'] = '已解决';
            }else if($v['allow'] == 1){
                $res_notice1[$k]['allow'] = '未解决';
            }

            $noticeid[] = $v['noticeid'];
        }

        $noticeid = array_unique($noticeid);

        $getid = implode(',',$noticeid);

        if($getid){
            $res_notice3 = M('noticecontent')->where('id in ('.$getid.')')->select();
        }

        $res_user = M('user')->where('id = "'.$id.'"')->find();
        foreach($res_notice1 as $k=>$v){

            $res_notice1[$k]['user'] = $res_user['user'];
            $res_notice1[$k]['userid'] = $res_user['id'];

            foreach($res_notice3 as $k1=>$v1){

                if($v['noticeid'] == $v1['id']){
                    $res_notice1[$k]['content'] = $v1['content'];

                }
            }
        }

//        var_dump($res_notice1);die;
//        $res_notice1 = [];

        $this->assign('notice',$res_notice1);
        $this->assign('page',$show);

        $this->display();

    }
}